//
//  XFXNewsHeadLineOneTableViewCell.m
//  XFXnews
//
//  Created by dllo on 16/1/28.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "XFXNewsHeadLineOneTableViewCell.h"
#import "XFXNewsImageModel.h"

@interface XFXNewsHeadLineOneTableViewCell ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, strong) NSMutableArray *allArray;
@property (nonatomic, retain) UILabel *title;
@property (nonatomic, retain) UILabel *replyCount;
@property (nonatomic, retain) UICollectionView *myCollectionView;

@end

@implementation XFXNewsHeadLineOneTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubViews];
    }return self;
}

- (void)createSubViews{
    
    self.title = [[UILabel alloc] initWithFrame:CGRectMake(XFXWIDTH * 0.01, XFXHEIGHT * 0.016, XFXWIDTH, 10 * XFXSHEI)];
    self.title.font = [UIFont systemFontOfSize:13.0];
    [self.contentView addSubview:self.title];
    
    self.replyCount = [[UILabel alloc] initWithFrame:CGRectMake(XFXWIDTH - 85 * XFXSWID, XFXHEIGHT * 0.016, XFXWIDTH, 10 * XFXSHEI)];
    self.replyCount.text = @"333";
    [self.contentView addSubview:self.replyCount];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake((XFXWIDTH - 50) / 3, XFXHEIGHT * 0.13);
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 10 * XFXSHEI, 10 * XFXSWID, 10 * XFXSHEI);
    flowLayout.minimumLineSpacing = 10;
    self.myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, self.title.frame.origin.y + self.title.frame.size.height + 5 * XFXSHEI, XFXWIDTH, XFXHEIGHT * 0.15) collectionViewLayout:flowLayout];
    self.myCollectionView.backgroundColor = [UIColor whiteColor];
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;
    
    [self.myCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.contentView addSubview:self.myCollectionView];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.array.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, (XFXWIDTH - 40) / 3 * XFXSWID, XFXHEIGHT * 0.15)];
    [cell.contentView addSubview:imageView];
    NSURL *url = [NSURL URLWithString:[self.array objectAtIndex:indexPath.row]];
    [imageView sd_setImageWithURL:url placeholderImage:nil];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.myDelegate pushDetail:self.model];
    
}
-(void)setModel:(XFXNewsHeadLineModel *)model{
    _model = model;
    self.title.text = _model.title;
    
    self.replyCount.text = [NSString stringWithFormat:@"%@跟帖",model.replyCount];
    self.replyCount.font = [UIFont systemFontOfSize:13.0];
    self.array = [NSMutableArray array];
    NSString *image = _model.imgsrc;
    [self.array addObject:image];
    
    for (int i = 0; i < _model.imgnewextra.count; i++) {
        NSMutableDictionary *dic = _model.imgnewextra[i];
        XFXNewsImageModel *ImageModel= [[XFXNewsImageModel alloc] init];
        [ImageModel setValuesForKeysWithDictionary:dic];
        [self.array addObject:ImageModel.imgsrc];
    }
    [self.myCollectionView reloadData];
}

- (void)setModelOne:(XFXNewsHeadLineModel *)modelOne{
    
    _modelOne = modelOne;
    self.title.text = _modelOne.title;
    
    self.replyCount.text = [NSString stringWithFormat:@"%@跟帖",_modelOne.replyCount];
    self.replyCount.font = [UIFont systemFontOfSize:13.0];
    self.array = [NSMutableArray array];
    NSString *image = _modelOne.imgsrc;
    [self.array addObject:image];
    for (int i = 0; i < _modelOne.imgnewextra.count; i++) {
        NSMutableDictionary *dic = _modelOne.imgnewextra[i];
        XFXNewsImageModel *ImageModel= [[XFXNewsImageModel alloc] init];
        [ImageModel setValuesForKeysWithDictionary:dic];
        [self.array addObject:ImageModel.imgsrc];
    }
    [self.myCollectionView reloadData];

    
}

@end
